package kuzmenkov.oip.adapters.byu.jigl;

import jigl.image.Image;
import jigl.image.io.ImageInputStreamJAI;
import jigl.image.io.ImageOutputStreamJAI;
import jigl.image.ops.HistogramEqualizer;
import kuzmenkov.oip.adapters.ImageProcessingAdapter;
import kuzmenkov.oip.common.FileTool;

import java.util.logging.Logger;

/**
 * This class adapts a histogram equalization module to use it by using
 * a web-interface
 * User: Michael Kuzmenkov
 * Date: 17.04.2009
 * Time: 23:17:40
 */
public class HistogramEqualizerAdapter extends ImageProcessingAdapter{
    private Logger log = Logger.getLogger(HistogramEqualizerAdapter.class.getName());

    /**
     * Equalizes a histogram and returns a new image with equalized histogram.
     * It uses LookupTable and CumulativeHistogram to implement equalization.
     * It takes only one parameter that is a path to a source image (java.lang.String).
     * It returns the path to the new image (java.lang.String). 
     * @param params the input parameters
     * @return the output parameters
     * @throws Exception
     */
    public Object[] process(Object[] params) throws Exception {
        String srcPath = (String) params[0];
        log.info("srcPath="+srcPath);

        FileTool ftool = FileTool.getInstance();
        srcPath = ftool.relPathToAbs(srcPath);

        ImageInputStreamJAI inputStream = new ImageInputStreamJAI(srcPath);
        Image srcImage = inputStream.read();

        HistogramEqualizer histEqualizer = new HistogramEqualizer();
        Image dstImage = histEqualizer.apply(srcImage);


        String dstFileName = "1.png";
        String absDstPath = ftool.getTempDir(getSessionId())+dstFileName;
        log.info("destination path="+absDstPath);
        ImageOutputStreamJAI outputStream = new ImageOutputStreamJAI(absDstPath);
        outputStream.writePNG(dstImage);

        String relDstPath = ftool.getRelativeTempDir(getSessionId())+dstFileName;
        log.info("relative destination path="+relDstPath);

        return new Object[]{relDstPath};
    }
}
